from PIL import Image
import pytesseract
 
def process_img(img: Image, dstImgPath: str):
    for i in range(img.size[0]):
        for j in range(img.size[1]):
            r, g, b = img.getpixel((i, j))
            # 将干扰线像素值大于等于100的变为纯白色
            if r >= 100 and g >= 100 and b >= 100:
                img.putpixel((i, j), (255, 255, 255))
        #img.save(f'img_new/{img}')
    #img.show(img)
    img.save(dstImgPath)
 
if __name__ == '__main__':
    dstImgPath= "D:\\picture\\p2.jpg"
    img = Image.open("D:\\picture\\p1.jpg")
    identifyCode = pytesseract.image_to_string((img), lang='eng')
    print("验证码：" + identifyCode)
    process_img(img, dstImgPath)
    img = Image.open(dstImgPath)
    identifyCode = pytesseract.image_to_string((img), lang='eng')
    print("处理后的验证码：" + identifyCode)
